<?php
/*
* @Descripttion: 配置信息
* @Author: jiosen <4631458@qq.com>
* @Date: 2024-06-12 11:06:35
*/
namespace app\service;

use think\facade\Config;

class Setting
{
    /**
     * 获取配置信息
     * @return array
     */
    static public function getConfig()
    {
        $config =  Config::get('bot.setting');

        $save = false;

        // 加上默认值
        if(!isset($config['name'])){
            $user = Bot::get('/get_login_info');
            if($user['state'] == 1){
                $config['name'] = $user['data']['nickname'];
                $save = true;
            }
        }

        !isset($config['is_open']) && $config['is_open'] = 0;
        !isset($config['is_open_private']) && $config['is_open_private'] = 0;

        if($save){
            self::setConfig($config);
        }

        return $config;
    }

    /**
     * 保存配置
     * @param $data
     * @return array
     */
    static public function setConfig($data)
    {
        // return Config::set(['setting'=>$data],'bot');
        $config = Config::get('bot');
        $config['setting'] = $data;
        $data = var_export($config, true);
        $content = "<?php\r\n\r\n/*机器人配置 本文件由程序自动生成（" . date('Y-m-d H:i:s') . "）*/\r\nreturn {$data};";

        return file_put_contents(app()->getConfigPath() . "bot.php", $content);
    }
}
